package com.ug.system.processor;

import com.ug.common.constant.HotConstants;
import com.ug.system.entity.AttributeValue;
import com.ug.system.entity.HtmlAttributeValue;
import org.springframework.stereotype.Component;
import us.codecraft.webmagic.Spider;
import us.codecraft.webmagic.processor.PageProcessor;

import java.util.List;

/**
 * @author: LY
 * @date: 2024/12/18 14:12
 * @description: 热点信息采集工厂
 */
@Component
public class HotToDayFactory {

    /**
     * 今日热点信息获取
     * @param type 渠道类型
     */
    public List<AttributeValue> getHotToDayProcessor(String type) {
        return switch (type) {
            case HotConstants.HOT_TYPE_BAIDU -> hotToDayProcessor(new BaiDuHotHtmlPageProcessor(), HotConstants.BAIDU_HOT_URL);
            case HotConstants.HOT_TYPE_BIBI -> BibiHotHtmlPageProcessor.hotToDayProcessor();
            case HotConstants.HOT_TYPE_DOUYIN -> DouyinHotHtmlPageProcessor.hotToDayProcessor();
            case HotConstants.HOT_TYPE_PENG_PAI -> PengpaiHotHtmlPageProcessor.hotToDayProcessor();
            case HotConstants.HOT_TYPE_JUE_JIN -> JuejinHotHtmlPageProcessor.hotToDayProcessor();
            case HotConstants.HOT_TYPE_WEIBO -> WeiboHotHtmlPageProcessor.hotToDayProcessor();
            case HotConstants.HOT_TYPE_TIBA -> TiebaHotHtmlPageProcessor.hotToDayProcessor();
            case HotConstants.HOT_TYPE_DOUBAN -> hotToDayProcessor(new DoubanHotHtmlPageProcessor(), HotConstants.DOUBAN_HOT_URL);
            case HotConstants.HOT_TYPE_DOUBAN_BEIMEI -> hotToDayProcessor(new DoubanbeimeiHotHtmlPageProcessor(), HotConstants.DOUBAN_BEIMEI_HOT_URL);
            case HotConstants.HOT_TYPE_TOUTIAO -> ToutiaoHotHtmlPageProcessor.hotToDayProcessor();
            case HotConstants.HOT_TYPE_ZHIHU -> ZhihuHotHtmlPageProcessor.hotToDayProcessor();
            case HotConstants.HOT_TYPE_POJIE -> hotToDayProcessor(new PojieHotHtmlPageProcessor(), HotConstants.POJIE_HOT_URL);
            default -> null;
        };
    }

    /**
     * 今日热点信息抓取
     */
    public List<AttributeValue> hotToDayProcessor(PageProcessor pageProcessor, String url) {
        HtmlAttributeValue pipeline = new HtmlAttributeValue();
        Spider.create(pageProcessor)
                //  从指定链接处开始抓
                .addUrl(url)
                //  结果保存
                .addPipeline(pipeline)
                //  开启5个线程抓取
                .thread(1)
                //  启动爬虫
                .run();
        return pipeline.getCollected();
    }
}
